(function($){
    /**
     * 获取光标位置.
     */
    $.fn.getCursorPosition = function() {
        var editorBox = $(this).get(0),
            index = 0;        
        if (document.selection) { //IE Support 
            editorBox.focus();
            var selectionRange = document.selection.createRange();
            if(editorBox.nodeName === 'TEXTAREA') { //textarea 
                var selectionRangeCloning = selectionRange.duplicate();
                selectionRangeCloning.moveToElementText(editorBox);
                var index = -1;
                while (selectionRangeCloning.inRange(selectionRange)) {
                    selectionRangeCloning.moveStart('character');
                    index++;
                };
            }else if(editorBox.nodeName === 'INPUT') { //input 
                selectionRange.moveStart('character', -editorBox.value.length);
                index = selectionRange.text.length;
            }
        }else if(!Type.isUndefined(editorBox.selectionStart)) { 
            index = editorBox.selectionStart;
        }
        return index;
    };
    
    /**
     * 设置光标位置.
     * @param {Number} index
     */
    $.fn.setCursorPosition = function(index) {    
        var editorBox = $(this).get(0);
        if (document.selection) { //IE Support
            var range = editorBox.createTextRange();
            range.collapse(true);
            range.moveStart('character', index);
            range.select();
        } else {
            editorBox.selectionStart = index;
            editorBox.selectionEnd = index;
        }
        return this;
    };
    
    /**
     * 选中文本. 
     * @param {Number} len
     * @param {Number} [start]
     */
    $.fn.selectTextByCursor = function(len, start) {
        var $editorBox = $(this),
            editorBox = $editorBox.get(0);        
        if(Type.isUndefined(start)){
            start = $editorBox.getCursorPosition(); 
        }
        if (document.selection) { //IE Support
            var range = editorBox.createTextRange();
            range.collapse(true);
            range.moveStart('character', start);
            range.moveEnd('character', len);
            range.select();
        } else {
            editorBox.selectionStart = start;
            editorBox.selectionEnd = start + len;
        }
        return this;
    };
    
    /**
     * 插入文本
     * @param {Number} text
     * @param {Number} [start]
     */
    $.fn.insertTextByCursor = function(text, start) {
        var $editorBox = $(this),
            editorBox = $editorBox.get(0),
            editorBoxVal = $editorBox.val();
        if(Type.isUndefined(start)){
            start = $editorBox.getCursorPosition(); 
        }
        if (editorBoxVal.length >= start) {
            var preStr = editorBoxVal.substring(0, start);
            var sufStr = editorBoxVal.substring(start);
            editorBox.value = preStr + text + sufStr;
        } else {
            start = editorBoxVal.length;
            editorBox.value = editorBoxVal + text;
        }
        $editorBox.setCursorPosition(start + text.length);
        return this;
    };
})(jQuery);